<template>
  <div>
    <van-nav-bar
      title="订单预览"
      left-arrow
      fixed
      placeholder
      @click-left="goBack"
    >
      <template #right>
        <van-icon name="search" size="18" />
      </template>
    </van-nav-bar>
    <div class="site-box" @click="router.push('/address')">
      <div class="site-box-left" v-if="address.is_default === 1">
        <p>{{ address.name }}：{{ address.phone }}</p>
        <p>
          地址：{{ address.province }}{{ address.city }}{{ address.county
          }}{{ address.address }}
        </p>
      </div>
      <p v-else @click="router.push('/address')">{{ address.address }}</p>
      <div class="site-box-arrow"><van-icon name="arrow" size="20px" /></div>
      <div class="site-border"></div>
    </div>
    <div class="order-list-box">
      <div class="list-item" v-for="(item, index) in orderList" :key="index">
        <div class="order-img">
          <img v-lazy="item.goods.cover_url" alt="" />
        </div>
        <div class="order-info">
          <div class="order-info-dh">
            <span>{{ item.goods.title }}</span>
            <span>{{ item.num }}</span>
          </div>
          <div class="order-info-db">
            <small>￥</small>{{ item.goods.price.toFixed(2) }}
          </div>
        </div>
      </div>
    </div>
    <van-submit-bar
      :price="total * 100"
      button-text="生成订单"
      @submit="onSubmit"
      >商品金额</van-submit-bar
    >
    <van-popup
      v-model:show="showPay"
      closeable
      position="bottom"
      :style="{ height: '30%' }"
      @close="closePay"
    >
      <div class="pay">
        <div style="width: 50%">
          <p>支付宝二维码</p>
          <img v-lazy="aliyun" alt="" style="padding: 10px 20px 0" />
        </div>
        <div style="width: 50%">
          <p>微信二维码</p>
          <img v-lazy="wechat" alt="" style="padding: 10px 20px 0" />
        </div>
      </div>
    </van-popup>
  </div>
</template>
<script>
import { useRouter } from 'vue-router'
import { orderProview, createOrder, payCode, payStatus } from '../../network/order'
import { computed, onMounted, reactive, toRefs } from 'vue'
import { Toast } from 'vant'
import { useStore } from 'vuex'
export default {
  setup () {
    const router = useRouter()
    const store = useStore()
    const state = reactive({
      address: {},
      orderList: [],
      showPay: false,
      orderId: 0,
      aliyun: '',
      wechat: ''
    })
    const init = () => {
      Toast.loading({ message: '加载中...', forbidClick: true })
      orderProview().then(res => {
        console.log(res)
        const address = res.address.filter(item => item.is_default === 1)
        if (address.length === 0) {
          state.address = { address: '还没有设置默认地址，请选择或者填写信息' }
        } else {
          state.address = address[0]
        }
        state.orderList = res.carts
        Toast.clear()
      })
    }
    onMounted(() => {
      init()
    })
    const onSubmit = () => {
      createOrder({ address_id: state.address.id }).then(res => {
        console.log(res)
        Toast('创建订单成功')
        state.showPay = true
        store.dispatch('storeAddcart')
        state.orderId = res.id
        payCode(state.orderId, { type: 'aliyun' }).then(res => {
          console.log(res)
          state.aliyun = res.qr_code_url
          state.wechat = res.qr_code_url
        })
        const time = setInterval(() => {
          payStatus(state.orderId).then(res => {
            if (res === 2) {
              clearInterval(time)
              router.push({ path: '/orderdetail', query: { id: state.orderId } })
            }
          })
        }, 2000)
      })
    }
    const closePay = () => {
      router.push({ path: '/orderdetail', query: { id: state.orderId } })
    }
    const total = computed(() => {
      let sum = 0
      state.orderList.forEach(item => {
        sum += parseFloat(item.goods.price) * parseInt(item.num)
      })
      return sum
    })
    const goBack = () => {
      router.go(-1)
    }
    return {
      goBack,
      ...toRefs(state),
      total,
      onSubmit,
      closePay,
      router
    }
  }
}
</script>
<style scoped lang="less">
.site-box {
  position: relative;
  margin-bottom: 20px;
  padding: 20px 15px;
  background-color: #f9f9f9;
  display: flex;
  justify-content: space-between;
  text-align: left;
  p {
    line-height: 30px;
  }
  .site-box-arrow {
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
  .site-border {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: -webkit-repeating-linear-gradient(
      135deg,
      #ff6c6c 0,
      #ff6c6c 20%,
      transparent 0,
      transparent 25%,
      #1989fa 0,
      #1989fa 45%,
      transparent 0,
      transparent 50%
    );
    background: repeating-linear-gradient(
      -45deg,
      #ff6c6c 0,
      #ff6c6c 20%,
      transparent 0,
      transparent 25%,
      #1989fa 0,
      #1989fa 45%,
      transparent 0,
      transparent 50%
    );
    background-size: 80px;
  }
}
.van-card {
  text-align: left;
}
.order-list-box {
  margin-bottom: 50px;
}
.list-item {
  display: flex;
  padding: 15px;
  .order-img {
    width: 100px;
    height: 100px;
    img {
      width: 100%;
    }
  }
  .order-info {
    flex: 1;
    display: flex;
    padding: 15px;
    flex-direction: column;
    justify-content: space-between;
    .order-info-dh {
      display: flex;
      justify-content: space-between;
    }
    .order-info-db {
      text-align: left;
      color: red;
    }
  }
}
.van-submit-bar {
  bottom: 50px;
}
.pay {
  margin-top: 15px;
  display: flex;
  justify-content: space-between;
}
</style>
